/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package zblibrary.facechat.modules.lookinggood;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import java.util.List;

import zblibrary.facechat.R;
import zblibrary.facechat.entity.LookingGoodEntity;
import zblibrary.facechat.modules.UserInfoActivity;
import zblibrary.facechat.modules.lookinggood.adapter.LookingGoodAdapter;
import zblibrary.facechat.modules.lookinggood.view.LookingGoodItemView;
import zblibrary.facechat.util.HttpRequest;
import zblibrary.facechat.util.TestUtil;
import zuo.biao.library.base.BaseHttpRecyclerFragment;
import zuo.biao.library.interfaces.AdapterCallBack;
import zuo.biao.library.interfaces.CacheCallBack;
import zuo.biao.library.util.JSON;

public class LookingGoodRecyclerFragment extends BaseHttpRecyclerFragment<LookingGoodEntity, LookingGoodItemView, LookingGoodAdapter> implements CacheCallBack<LookingGoodEntity> {
	private static final String TAG = "FoundGoodRecyclerFragment";
	private LookingGoodViewModel viewModel;
	public static LookingGoodRecyclerFragment createInstance() {
		LookingGoodRecyclerFragment fragment = new LookingGoodRecyclerFragment();
		Bundle bundle = new Bundle();
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		initCache(this);
		//功能归类分区方法，必须调用<<<<<<<<<<
		initView();
		initData();
		initEvent();
		//功能归类分区方法，必须调用>>>>>>>>>>
		srlBaseHttpRecycler.autoRefresh();
		return view;
	}
	@Override
	public void initView() {//必须调用
		super.initView();
		rvBaseRecycler.setBackgroundColor(getResources().getColor(R.color.white_alpha_6));
	}

	@Override
	public void setList(final List<LookingGoodEntity> list) {
		setList(new AdapterCallBack<LookingGoodAdapter>() {
			@Override
			public LookingGoodAdapter createAdapter() {
				return new LookingGoodAdapter(context);
			}
			@Override
			public void refreshAdapter() {
				adapter.refresh(viewModel.getlookingGoodList());
			}
		});
	}
	//UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	//Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

	@Override
	public void initData() {//必须调用
		super.initData();
		viewModel = new LookingGoodViewModel();
		viewModel.initData();
	}

	@Override
	public void getListAsync(final int page) {
		HttpRequest.getFindGoodList(-page, this);
		//实际使用时用这个，需要配置服务器地址		HttpRequest.getUserList(range, page, -page, this);
//		setList(viewModel.getFoundGoodList());
		//仅测试用<<<<<<<<<<<
//		new Handler().postDelayed(new Runnable() {
//			@Override
//			public void run() {
//				onHttpResponse(-page, page >= 5 ? null : JSON.toJSONString(TestUtil.getDiscussList(page, getCacheCount())), null);
//			}
//		}, 1000);
//		//仅测试用>>>>>>>>>>>>
	}

	@Override
	public List<LookingGoodEntity> parseArray(String json) {
		return JSON.parseArray(json, LookingGoodEntity.class);
	}


	@Override
	public Class<LookingGoodEntity> getCacheClass() {
		return LookingGoodEntity.class;
	}
	@Override
	public String getCacheGroup() {
		return "range=" + 1;
	}

	@Override
	public String getCacheId(LookingGoodEntity data) {
		return data == null ? null : "" + data.getId();
	}
	@Override
	public int getCacheCount() {
		return 10;
	}

	//Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	//Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	@Override
	public void initEvent() {//必须调用
		super.initEvent();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if (id > 0) {
			toActivity(UserInfoActivity.createIntent(context, id));
		}
	}
	//生命周期、onActivityResult<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	//生命周期、onActivityResult>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	//Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
}